package com.app.web.order.job;

import com.alibaba.fastjson.JSON;
import com.app.utils.base.j2se.*;
import com.app.utils.task.TaskDao;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description  订单分佣明细固化数据
 * Created by 王伟
 * Date create on 15:28 2019/12/12.
 */
@Component
public class SyncOrderExpect implements Job {
    @Autowired
    private TaskDao taskDao;
    @Autowired
    private OrderAnalyDao orderAnalyDao;

    //源表 基础数据表
    private String source_t_goods_order="t_goods_order&t_analy_order_promotion_details";
    //分析表 订单分佣明数据固化表
    private String analy_goods_order_expect_detail="t_goods_order_expect_detail";

    @Override
    @Transactional
    public void execute(JobExecutionContext context) throws JobExecutionException {

        System.out.println("*****begin*****"+ DateUtil.getDatetime()+"-分析订单分佣明细固化数据-OrderPromotionDetails");
        final long[] record_end = {0};
        try{
            //获取记录值
            long record_begin = taskDao.getRecord(source_t_goods_order, analy_goods_order_expect_detail);
            //获取订单数据
            List<Map<String, Object>> orderList = orderAnalyDao.getOrderPromotionExpectList(record_begin);
            orderList.forEach(x->{
                record_end[0] = Convert.objToLong(x.get("expect_id"));
                x.put("day",Convert.objToStr(x.get("date")).replaceAll("-",""));
                orderAnalyDao.addOrderExpect(x);
            });
            if(record_end[0]>record_begin){
                taskDao.updateRecord(record_end[0],source_t_goods_order,analy_goods_order_expect_detail);
            }
        }catch (Exception e){
            Map<String,Object> map = new HashMap<>();
            map.put("module",this.getClass().getName());
            map.put("methods",Thread.currentThread().getStackTrace()[1].getMethodName());
            map.put("desc","分析订单分佣明细固化数据");
            map.put("json","记录值="+ record_end[0]);
//            map.put("exce", Logger.exceptionToString(e));
            /* TODO 发送钉钉推送 */
//            logService.taskLog(map);
            Logger.error(String.format("分析订单分佣明细固化数据异常，param:%s", JSON.toJSONString(map)),e);
            throw new RuntimeException("分析订单分佣明细固化数据");
        }
        System.out.println("*****end*****"+ DateUtil.getDatetime()+"-分析订单分佣明细固化数据-OrderPromotionDetails");
        System.out.println();
    }
}
